<?php
include_once $_SERVER["DOCUMENT_ROOT"].'/service/entite/Entite.php';
include_once $_SERVER["DOCUMENT_ROOT"].'/service/entite/photo/PhotoEntite.php';
include_once $_SERVER["DOCUMENT_ROOT"].'/outils/database/Database.php';
include_once $_SERVER["DOCUMENT_ROOT"].'/outils/exceptions/DAOException.php';
include_once $_SERVER["DOCUMENT_ROOT"].'/persistence/albumphoto/AlbumPhotoDAO.php';

class PhotoDAO {
	
	private function PhotoDAO() {
		//évite la construction
	}
		
	/*
	 * Récupère la liste de toutes les photos
	 */
	public static function loadListePhotoAlbum(AlbumPhotoEntite $albumPhotoEntite) {
		
		$result = Database::getInstance()->executeQueryAndClose("SELECT id, id_albumphoto,nomfichier,commentaire FROM photo WHERE id_albumphoto = ".$albumPhotoEntite->id." ORDER BY id_albumphoto,nomfichier");
		$albumPhotoEntite->listePhoto = Array();
		foreach ($result as $row) {
		//foreach ($result as $row) {
			$albumPhotoEntite->listePhoto[] = PhotoDAO::getPhotoFromDatabaseRow($row, $albumPhotoEntite);
		}
		return $albumPhotoEntite;
	}
	
	public static function getPhotoByID($idPhoto) {
		
		$result = Database::getInstance()->executeQueryAndClose("SELECT id, id_albumphoto,nomfichier,commentaire FROM photo WHERE id = $idPhoto");
		$photo = null;
		foreach ($result as $row) {
		//foreach ($result as $row) {
			$albumPhotoEntite = AlbumPhotoDAO::getAlbumPhotoByID($row['id_albumphoto']);
			$photo = PhotoDAO::getPhotoFromDatabaseRow($row, $albumPhotoEntite);
		}
		return $photo;
	}
	
	public static function getPhotoByNomFichierAndIDAlbumPhoto($idAlbumPhoto, $nomFichier) {
		
		$result = Database::getInstance()->executeQueryAndClose("SELECT id, id_albumphoto,nomfichier,commentaire FROM photo WHERE nomfichier = '$nomFichier' AND id_albumphoto = $idAlbumPhoto");
		$photo = null;
		foreach ($result as $row) {
		//foreach ($result as $row) {
			$albumPhotoEntite = AlbumPhotoDAO::getAlbumPhotoByID($row['id_albumphoto']);
			$photo = PhotoDAO::getPhotoFromDatabaseRow($row, $albumPhotoEntite);
		}
		return $photo;
	}
	
	public static function save(PhotoEntite $photoEntite) {
		if ($photoEntite->albumPhoto == null) {
			throw new DAOException("Tentative de persister une photo sans album : ".$photoEntite->nomFichier);
		}
		if($photoEntite->id == Entite::getUnpersistedId()) {
			$albumPhoto = $photoEntite->albumPhoto;
			$result = PhotoDAO::insert($albumPhoto->id, $photoEntite);
		} else {
			$result = PhotoDAO::update($photoEntite);
		}
		if ($result != 1) {
			$titre = $photoEntite->nomFichier;
			throw new DAOException("La sauvegarde de l'entite $titre a mis à jour $result lignes");
		}
	}
	
	public static function delete($id) { 
		
		$result = Database::getInstance()->executeUpdateQueryAndClose("DELETE FROM photo WHERE id = $id") ;
		return $result;
	}
	
	public static function deleteFromNomFichier($idAlbumPhoto, $nomFichier) {
		$photoEntite = PhotoDAO::getPhotoByNomFichierAndIDAlbumPhoto($idAlbumPhoto, $nomFichier);
		if ($photoEntite == null) {
			throw new DAOException("Impossible de supprimer l'enregistrement de la photo '$nomFichier' dans l'album '$idAlbumPhoto' : L'enregistrement n'a pas été trouvé");
		}
		$result = PhotoDAO::delete($photoEntite->id);
		return $result;
	}
	
	private static function insert($idAlbumPhoto, PhotoEntite $photoEntite) {
		
		$nomFichier = mysql_escape_string($photoEntite->nomFichier);
		$commentaire = mysql_escape_string($photoEntite->commentaire);
		$result = Database::getInstance()->executeUpdateQueryAndClose("INSERT INTO photo (id_albumphoto,nomfichier,commentaire) values ($idAlbumPhoto,'$nomFichier','$commentaire')") ;
		return $result;
	}
	
	private static function update(PhotoEntite $photoEntite) {
		
		$commentaire = mysql_escape_string($photoEntite->commentaire);
		$result = Database::getInstance()->executeUpdateQueryAndClose("UPDATE photo SET commentaire = '$commentaire' WHERE id = $photoEntite->id");
		return $result;
	}
	
	private static function getPhotoFromDatabaseRow($row, AlbumPhotoEntite $albumPhotoEntite) {
		$photoEntite = new PhotoEntite();
		$photoEntite->id = $row['id'];
		$photoEntite->nomFichier = $row['nomfichier'];
		$photoEntite->commentaire = $row['commentaire'];
		$photoEntite->albumPhoto = $albumPhotoEntite;
		return $photoEntite;
	}
}
?>